package Stdlog::Logger;

use 5.008;
use strict;
use warnings;
use Hash::Util qw(lock_keys);
use Stdlog::Criteria;
use Stdlog::Formatter;
use Stdlog::Writer;


sub new {
    my ($pkg, $conf) = @_;
    my $self = {
        'name'     => $conf->{name} || '',
        'conf'     => $conf,
        'continue' => 1,
        'criteria' => undef,
        'format'   => undef,
        'writer'   => undef,
    };

    bless $self, $pkg;
    $self->criteria($conf);
    $self->format($conf);
    $self->writer($conf);

    if( exists $conf->{continue} ){
        $self->{continue} = 0  if $conf->{continue} =~ /^[0FfNn]/;
    }
    
    lock_keys(%$self);
    return $self;
}

# Arguments are:
#   $self -- the Stdlog::Logger object
#   $args -- severity
#   @msgs -- an array of messages
sub logmsg {
    my $self = shift @_;
    my $args = shift @_;
    if( $self->{criteria}->($args) ){
        $self->{writer}->logmsg($args, $self->{format}->($args, @_));
    }
    return $self->{continue};
}


sub conf {
    my ($self, $conf) = @_;
    if( defined $conf ){
        $self->{conf} = $conf;
    }
    return $self->{conf};
}


sub name {
    my ($self, $name) = @_;
    if( defined $name ){
        $self->{name} = $name;
    }
    return $self->{name};
}


sub continue {
    my ($self, $cont) = @_;
    if( defined $cont ){
        $self->{continue} = $cont;
    }
    return $self->{continue};
}


sub criteria {
    my ($self, $criteria) = @_;
    if( defined $criteria ){
        if ( UNIVERSAL::isa($criteria, 'Stdlog::Criteria') ) {
            $self->{criteria} = $criteria;
        } else {
            $self->{criteria} = Stdlog::Criteria->new($criteria);
        }
    }
    return $self->{criteria};
}

sub set_criteria {
    my ($self, $name, $value) = @_;
    $self->{conf}{$name} = $value;
    $self->{criteria} = Stdlog::Criteria->new($self->{conf});
}


sub format {
    my ($self, $format) = @_;
    if( defined $format ){
        if ( UNIVERSAL::isa($format, 'Stdlog::Format') ) {
            $self->{format} = $format;
        } else {
            $self->{format} = Stdlog::Formatter->new($format);
        }
    }
    return $self->{format};
}


sub writer {
    my ($self, $writer) = @_;
    if( defined $writer ){
        if ( UNIVERSAL::isa($writer, 'Stdlog::Writer') ) {
            $self->{writer} = $writer;
        } else {
            $self->{writer} = Stdlog::Writer->new($writer);
        }
    }
    return $self->{writer};
}


sub priority {
    my ($self, $idx) = @_;
    my $loggers = Stdlog->get_loggers;
    if ( defined $idx ) {
        my $cur_idx = $self->get_priority;
        if ( defined $cur_idx && $idx >= 0 && $idx < scalar @$loggers ) {
            delete $loggers->[$cur_idx];
            splice @$loggers, $idx, 1, $self;
        }
    }else{
        for ( my $i = 0; $i < @$loggers; $i++ ) {
            return $i if $loggers->[$i] eq $self;
        }
        return undef;
    }
}


1;
__END__

=head1 NAME

Stdlog::Writer -- configuration management for Stdlog

=head1 SYNOPSIS

  use Stdlog;
  

=head1 DESCRIPTION


=head1 CLASS METHODS

=head2 Stdlog::Writer->new(CLASS, OPTS)


=head1 OBJECT METHODS



=head1 COOKBOOK

=head2 Change the severity criteria

    my $conf = $logger->conf;
    $conf->{severity} = 'NOTIFY+';
    $logger->severity($conf);
    
=head1 EXPORTS

None by default.


=head1 SEE ALSO


=head1 AUTHOR

Robert Mah E<lt>rmah@pobox.comE<gt>


=head1 COPYRIGHT AND LICENSE

Copyright (C) 2007 by Robert Mah

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.8 or,
at your option, any later version of Perl 5 you may have available.

=cut
